package com.ziq.base.base

import android.app.Dialog
import android.content.Context
import android.os.Build
import android.os.Bundle
import android.view.Gravity
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.view.animation.*
import androidx.fragment.app.DialogFragment

open class BaseDialogFragment() : DialogFragment() {

    var mAnimationDuration = 200
    var mIsAnimating = false
    var animateTargetView :View? = null

    override fun dismiss() {
        if(animateTargetView != null){
            if (mIsAnimating) {
                return
            }
            animateHide()
        }else {
            super.dismiss()
        }
    }

    open fun dialogDismiss(){
        super.dismiss()
    }

    open fun getShowAnimationSet(): AnimationSet{
        val translate =
            TranslateAnimation(
                Animation.RELATIVE_TO_SELF,
                0f,
                Animation.RELATIVE_TO_SELF,
                0f,
                Animation.RELATIVE_TO_SELF,
                1f,
                Animation.RELATIVE_TO_SELF,
                0f
            )
        val alpha = AlphaAnimation(0f, 1f)
        val set = AnimationSet(true)
        set.addAnimation(translate)
        set.addAnimation(alpha)
        return set
    }

    open fun animateShow(target :View? = null) {
        this.animateTargetView = target
        if(animateTargetView != null){
            val set = getShowAnimationSet()
            set.interpolator = DecelerateInterpolator()
            set.duration = mAnimationDuration.toLong()
            set.fillAfter = true
            animateTargetView?.startAnimation(set)
        }
    }

    open fun getHideAnimationSet(): AnimationSet{
        val translate =
            TranslateAnimation(
                Animation.RELATIVE_TO_SELF,
                0f,
                Animation.RELATIVE_TO_SELF,
                0f,
                Animation.RELATIVE_TO_SELF,
                0f,
                Animation.RELATIVE_TO_SELF,
                1f
            )
        val alpha = AlphaAnimation(1f, 0f)
        val set = AnimationSet(true)
        set.addAnimation(translate)
        set.addAnimation(alpha)
        return set
    }

    open fun animateHide() {
        val dismissTask = Runnable {
            // java.lang.IllegalArgumentException: View=com.android.internal.policy.PhoneWindow$DecorView{22dbf5b V.E...... R......D 0,0-1080,1083} not attached to window manager
            // 在dismiss的时候可能已经detach了，简单try-catch一下
            try {
                dialogDismiss()
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        if (animateTargetView?.height == 0) {
            // TranslateAnimation will not call onAnimationEnd if its height is 0.
            // At this case, we run dismiss task immediately.
            dismissTask.run()
            return
        }
        val set = getHideAnimationSet()
        set.interpolator = DecelerateInterpolator()
        set.duration = mAnimationDuration.toLong()
        set.fillAfter = true
        set.setAnimationListener(object : Animation.AnimationListener {
            override fun onAnimationStart(animation: Animation) {
                mIsAnimating = true
            }

            override fun onAnimationEnd(animation: Animation) {
                mIsAnimating = false
                animateTargetView?.post(dismissTask)
            }

            override fun onAnimationRepeat(animation: Animation) {}
        })
        animateTargetView?.startAnimation(set)
    }

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return FullScreenDialog(requireContext())
    }

    class FullScreenDialog(context: Context) : BaseDialog(context) {

        override fun initHeight(): Int {
            return ViewGroup.LayoutParams.MATCH_PARENT
        }
        override fun initWidth(): Int {
            return ViewGroup.LayoutParams.MATCH_PARENT
        }

        override fun initGravity(): Int {
            return Gravity.BOTTOM or Gravity.CENTER
        }

        override fun getShowAnimationSet(): AnimationSet {
            val translate =
                TranslateAnimation(
                    Animation.RELATIVE_TO_SELF,
                    0f,
                    Animation.RELATIVE_TO_SELF,
                    0f,
                    Animation.RELATIVE_TO_SELF,
                    -1f,
                    Animation.RELATIVE_TO_SELF,
                    0f
                )
            val alpha = AlphaAnimation(0f, 1f)
            val set = AnimationSet(true)
            set.addAnimation(translate)
            set.addAnimation(alpha)
            return set
        }

        override fun getHideAnimationSet(): AnimationSet {
            val translate =
                TranslateAnimation(
                    Animation.RELATIVE_TO_SELF,
                    0f,
                    Animation.RELATIVE_TO_SELF,
                    0f,
                    Animation.RELATIVE_TO_SELF,
                    0f,
                    Animation.RELATIVE_TO_SELF,
                    -1f
                )
            val alpha = AlphaAnimation(1f, 0f)
            val set = AnimationSet(true)
            set.addAnimation(translate)
            set.addAnimation(alpha)
            return set
        }


        override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
            window?.decorView?.let {
                window?.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS)
                window?.decorView?.systemUiVisibility = View.SYSTEM_UI_FLAG_LAYOUT_STABLE or View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                window?.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
                    window?.decorView?.systemUiVisibility =
                        window?.decorView?.systemUiVisibility!!  or View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR
                    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                        window?.decorView?.systemUiVisibility =
                            window?.decorView?.systemUiVisibility!! or View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR
                    }
                }
            }

        }

    }
}